package edu.scau.mis.web.pos.controller;

import edu.scau.mis.core.utils.Result;
import edu.scau.mis.pos.entity.PosItem;
import edu.scau.mis.pos.service.IPosOrderItemService;
import edu.scau.mis.pos.entity.PosOrderItem;
import edu.scau.mis.pos.vo.ItemVo;
import edu.scau.mis.pos.vo.OrderItemVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * (OrderItem)表控制层
 *
 * @author makejava
 * @since 2021-11-06 21:31:24
 */
@RestController
@RequestMapping("/orderItem")
public class PosOrderItemController {
    /**
     * 服务对象
     */
    @Resource
    private IPosOrderItemService orderItemService;

    /**
     * 分页查询
     *
     * @param posOrderItem 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public List<PosOrderItem> queryAll(PosOrderItem posOrderItem) {
        return orderItemService.queryAll(posOrderItem);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public PosOrderItem queryById(@PathVariable("id") Integer id) {
        return orderItemService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param posOrderItem 实体
     * @return 新增结果
     */
    @PostMapping
    public PosOrderItem add(PosOrderItem posOrderItem) {
        return orderItemService.insert(posOrderItem);
    }

    /**
     * 编辑数据
     *
     * @param posOrderItem 实体
     * @return 编辑结果
     */
    @PutMapping
    public PosOrderItem edit(PosOrderItem posOrderItem) {
        return orderItemService.update(posOrderItem);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public Boolean deleteById(Integer id) {
        return orderItemService.deleteById(id);
    }

    @GetMapping("/openMapCache")
    @ApiOperation("点击开始销售按钮时调用，用于创建订单")
    public Result openMapCache(){
        //获取订单证号
        String orderNo = orderItemService.getOrderNo();
        if(!orderItemService.openMapCache(orderNo)){
            return Result.fail("订单创建失败！");
        }
        return Result.succ("订单创建成功！", orderNo);
    }

    @GetMapping("/enterItem")
    @ApiOperation("下单接口")
    public Result enterItem(OrderItemVo orderItemVo){
        //判断库存是否充足
        if(!orderItemService.isInventoryEnough(orderItemVo)){
            return Result.fail("库存不足", orderItemService.getPreviousOrderInfo(orderItemVo.getOrderNo()));//库存不足返回原先订单列表
        }
        //下单操作
        orderItemService.enterLineOrderItem(orderItemVo);

        return Result.succ(orderItemService.getOrderInformationByOrderNo(orderItemVo.getOrderNo()));
    }

    /*
    @GetMapping("/getOrderInfo")
    @ApiOperation("查看统计总额接口")
    public Result getOrderInfo(@RequestParam("orderNo") String orderNo){
        if(orderItemService.getOrderInformationByOrderNo(orderNo) == null){
            Result.fail("尚未创建订单，查询总数失败！");
        }
        return Result.succ(orderItemService.getOrderInformationByOrderNo(orderNo));
    }
    */

    @GetMapping("/confirmPay")
    @ApiOperation("确认下单接口")
    public Result ConfirmPayOrder(@RequestParam("orderNo") String orderNo){
        if(orderItemService.ConfirmPayItem(orderNo)){
            Result.fail("下单失败！");
        }
        return Result.succ("下单成功！");
    }

}

